package org.whh.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.whh.auth.WhhPermission;
import org.whh.base.util.ResultDTO;
import org.whh.org.domain.Department;
import org.whh.org.service.IDepartmentService;
import org.whh.base.util.AjaxResult;
import org.whh.base.util.QueryVo;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门管理",tags = "部门管理")
@WhhPermission(name="部门管理",desc = "我是部门管理")
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;
    @PutMapping
    @ApiOperation(value = "添加或者更新方法")
    @WhhPermission(name="添加或者修改",desc = "我是添加或者修改")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        System.out.println("de"+department);
        try {
            if(department.getId()!=null){
                departmentService.update(department);
            }else {
                departmentService.insert(department);
            }
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("操作出错"+e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除方法")
    @WhhPermission(name="删除",desc = "我是删除")
    public AjaxResult delete(@PathVariable("id") @ApiParam(value = "主键",required = true) Long id){
        try {
            departmentService.deleteById(id);
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个部门方法")
    @WhhPermission(name="通过id查询",desc = "我是通过id查询")
    public AjaxResult getById(@ApiParam(value = "主键",required = true) @PathVariable("id") Long id){
        try {
            Department department = departmentService.selectById(id);
            return new AjaxResult().getself().setObj(department);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有部门方法")
    public AjaxResult getAll(){
        try {
            List<Department> departments = departmentService.getAll();
            return new AjaxResult().getself().setObj(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "查询所有父部门方法")
    public AjaxResult getTree(){
        try {
            List<Department> departments = departmentService.getTree();
            return new AjaxResult().getself().setObj(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }
    @PostMapping
    @ApiOperation(value = "分页查询部门方法")
    public AjaxResult getPage(@RequestBody QueryVo queryVo){
        try {
            System.out.println(queryVo);
            ResultDTO<Department> result = departmentService.getAllPage(queryVo);
            return new AjaxResult().getself().setObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("分页查询出错"+e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除方法")
    public AjaxResult batchDelete(@ApiParam(value = "主键",required = true)@RequestBody List<Long> ids){
        try {
            System.out.println("批量删除方法");
            departmentService.batchDelete(ids);
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }
}
